package com.cvmars.jianyou.module.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.model.UserListModel;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.model.UserRelativeModel;
import com.cvmars.jianyou.module.adapter.UserFollowingAdapter;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FollowActivity extends BaseActivity {

    @BindView(R.id.list_follow)
    PulltoRefreshRecyclerView listFollow;
    UserFollowingAdapter adapter;

    @BindView(R.id.txt_title)
    TextView txt_title;

    List<UserRelativeModel> list = new ArrayList<>();

    boolean isFollowMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow);
        ButterKnife.bind(this);

        adapter = new UserFollowingAdapter(this, R.layout.item_follow, list);
        listFollow.setAdapter(adapter);
        isFollowMe = getIntent().getBooleanExtra("followme", false);
        if (isFollowMe) {
            txt_title.setText("关注我的");
            onFollowing();
        } else {

            onFollowAll();
        }

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Bundle bundle = new Bundle();
                bundle.putString("userid", list.get(position).customer.id);
                goActivity(bundle, PersonHomeActivity.class);
            }
        });

        listFollow.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {
                if (isFollowMe) {

                    onFollowing();
                } else {

                    onFollowAll();
                }
            }

            @Override
            public void onRefresh() {

                listFollow.mCurPager = 1;
                if (isFollowMe) {

                    onFollowing();
                } else {

                    onFollowAll();
                }
            }
        });

        getLoadDialogAndShow();
    }


    @Override
    protected View setLoadingWrapView() {
        return findViewById(R.id.list_follow);
    }

    private void onFollowing() {

        HttpUtils.getInstance().toSubscribe(Api.getInstance().getFollowingMeList(listFollow.mCurPager)
                , new SimpleSubscriber<HttpResult<UserListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<UserListModel> o) {
                        UserListModel data = o.getData();
                        showContent();
                        getLoadDialogAndDismiss();

                        if (listFollow.mCurPager == 1) {

                            list.clear();
                        }

                        if (data != null) {

                            list.addAll(data.results);

                            if(list.size() ==0){
                                showEmpty();
                            }
                            adapter.notifyDataSetChanged();
                            listFollow.refreshComplete();
                            listFollow.loadMoreComplete();

                            if (data.next == null) {

                                listFollow.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }


    private void onFollowAll() {

        HttpUtils.getInstance().toSubscribe(Api.getInstance().getFollowinglist(listFollow.mCurPager)
                , new SimpleSubscriber<HttpResult<UserListModel>>() {
                    @Override
                    protected void _onNext(HttpResult<UserListModel> o) {
                        UserListModel data = o.getData();
                        showContent();
                        getLoadDialogAndDismiss();

                        if (listFollow.mCurPager == 1) {

                            list.clear();
                        }

                        if (data != null) {

                            list.addAll(data.results);

                            if(list.size() ==0){
                                showEmpty();
                            }
                            adapter.notifyDataSetChanged();
                            listFollow.refreshComplete();
                            listFollow.loadMoreComplete();

                            if (data.next == null) {

                                listFollow.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }
}
